package com.novellotus.iov.carvice.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;


import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.DiagnosisValueAdapter;
import com.novellotus.iov.carvice.bean.RxDiagnosisAlarm;
import com.novellotus.iov.carvice.utils.MyViewUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rich on 2017/10/20.
 */

public class DialogChooseValue extends Dialog {
    private ChooseValueInterface chooseValueInterface;
    private List<RxDiagnosisAlarm.AlarmsBean.CausesBean> causeBeanList;
    private List<RxDiagnosisAlarm.AlarmsBean.SchemesBean> schemeBeanList;
    private Context context;
    private Activity activity;
    private int position;
    private boolean hasCauseOther, hasSchemesOther;//是有传过来了其他选项的保存值

    boolean hasCauses;//是否选择了报警原因的其他选项
    boolean hasSchemes;//是否选择了建议方案其他选项
    private String otherCause, schemesOther;

    public DialogChooseValue(@NonNull Context context, int theme) {
        super(context, theme);
        this.context = context;
    }


    public void initCauses(ChooseValueInterface clickTipsInterface, List<RxDiagnosisAlarm.AlarmsBean.CausesBean> causesBeanList, final int position, boolean hasCauseOther, String otherCause) {
        this.chooseValueInterface = clickTipsInterface;
        this.hasCauseOther = hasCauseOther;
        this.position = position;
        this.otherCause = otherCause;
        addOtherCause(causesBeanList);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_choose, null);
        TextView tv_tittle = view.findViewById(R.id.tvTitle);
        Button btnSubmit = view.findViewById(R.id.btnSubmit);
        Button btnCancel = view.findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseValueInterface.chooseCausesValue(position, getCause(), getCauseCode(), hasCauses, causeBeanList.get(causeBeanList.size() - 1).getCause());
                dismiss();
            }
        });
        tv_tittle.setText("请选择报警原因");
        RecyclerView recyclerView = view.findViewById(R.id.recycler);
        recyclerView.setLayoutManager(MyViewUtils.getLinearLayoutManager(context));
        recyclerView.setAdapter(new DiagnosisValueAdapter(this.causeBeanList, null));
        setCanceledOnTouchOutside(false);
        setContentView(view);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0; // 新位置X坐标
        lp.y = 0; // 新位置Y坐标
        lp.width = context.getResources().getDisplayMetrics().widthPixels; // 宽度
        view.measure(0, 0);
        lp.height = context.getResources().getDisplayMetrics().heightPixels / 2;
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.setAttributes(lp);
        show();
    }


    public void initSchemes(ChooseValueInterface clickTipsInterface, List<RxDiagnosisAlarm.AlarmsBean.SchemesBean> schemesBeanList, final int position, boolean hasSchemesOther, String schemesOther) {
        this.chooseValueInterface = clickTipsInterface;
        this.hasSchemesOther = hasSchemesOther;
        this.position = position;
        this.schemesOther = schemesOther;


        addOtherSchemes(schemesBeanList);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_choose, null);
        TextView tv_tittle = view.findViewById(R.id.tvTitle);
        Button btnSubmit = view.findViewById(R.id.btnSubmit);
        Button btnCancel = view.findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                chooseValueInterface.chooseSchemesValue(position, getSchemes(), getSchemesCode(), hasSchemes, schemeBeanList.get(schemeBeanList.size() - 1).getScheme());
                dismiss();
            }
        });
        tv_tittle.setText("请选择处理方案");
        RecyclerView recyclerView = view.findViewById(R.id.recycler);
        recyclerView.setLayoutManager(MyViewUtils.getLinearLayoutManager(context));
        recyclerView.setAdapter(new DiagnosisValueAdapter(null, this.schemeBeanList));
        //  getWindow().setBackgroundDrawableResource(android.R.color.transparent); //设置对话框背景透明 ，对于AlertDialog 就不管用了
        setCanceledOnTouchOutside(false);
        setContentView(view);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0; // 新位置X坐标
        lp.y = 0; // 新位置Y坐标
        lp.width = context.getResources().getDisplayMetrics().widthPixels; // 宽度
        view.measure(0, 0);
        lp.height = context.getResources().getDisplayMetrics().heightPixels / 2;
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.setAttributes(lp);
        show();
    }

    private void addOtherCause(List<RxDiagnosisAlarm.AlarmsBean.CausesBean> causesBeanList) {

        List<RxDiagnosisAlarm.AlarmsBean.CausesBean> causesList = new ArrayList<>();
        causesList.addAll(causesBeanList);
        RxDiagnosisAlarm.AlarmsBean.CausesBean causesBean = new RxDiagnosisAlarm.AlarmsBean.CausesBean();
        causesBean.setCauseCheck(hasCauseOther ? 1 : 0);
        causesBean.setCause(hasCauseOther ? otherCause : "其他");
        causesList.add(causesBean);
        this.causeBeanList = causesList;
    }

    private void addOtherSchemes(List<RxDiagnosisAlarm.AlarmsBean.SchemesBean> schemesBeanList) {
        List<RxDiagnosisAlarm.AlarmsBean.SchemesBean> schemesList = new ArrayList<>();
        schemesList.addAll(schemesBeanList);
        RxDiagnosisAlarm.AlarmsBean.SchemesBean schemesBean = new RxDiagnosisAlarm.AlarmsBean.SchemesBean();

        schemesBean.setSchemeCheck(hasSchemesOther ? 1 : 0);
        schemesBean.setScheme(hasSchemesOther ? schemesOther : "其他");
        schemesList.add(schemesBean);
        this.schemeBeanList = schemesList;

    }

    private String getCause() {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < causeBeanList.size(); i++) {
            if (causeBeanList.get(i).getCauseCheck() == 1) {
                if (str.length() == 0) {
                } else {
                    str.append("、");
                }
                str.append(causeBeanList.get(i).getCause());
                if (i == causeBeanList.size() - 1) {
                    hasCauses = !causeBeanList.get(i).getCause().equals("其他") && causeBeanList.get(i).getCause().length() > 0;
                }
            }

        }
        return str.toString();
    }

    private List<String> getCauseCode() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < causeBeanList.size(); i++) {
            if (causeBeanList.get(i).getCauseCheck() == 1) {
                list.add(causeBeanList.get(i).getCode());
            }

        }
        return list;
    }

    private String getSchemes() {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < schemeBeanList.size(); i++) {
            if (schemeBeanList.get(i).getSchemeCheck() == 1) {
                if (str.length() == 0) {
                } else {
                    str.append("、");
                }
                str.append(schemeBeanList.get(i).getScheme());
                if (i == schemeBeanList.size() - 1) {
                    hasSchemes = !schemeBeanList.get(i).getScheme().equals("其他") && schemeBeanList.get(i).getScheme().length() > 0;
                }
            }

        }
        return str.toString();
    }

    private List<String> getSchemesCode() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < schemeBeanList.size(); i++) {
            if (schemeBeanList.get(i).getSchemeCheck() == 1) {
                list.add(schemeBeanList.get(i).getCode());
            }

        }
        return list;
    }

    public interface ChooseValueInterface {
        void chooseCausesValue(int position, String value, List<String> code, boolean other, String otherValue);

        void chooseSchemesValue(int position, String value, List<String> code, boolean other, String otherValue);
    }

}
